﻿using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using CommonLib;
using System.Management;

namespace BurnSys
{
    public class MachineCode
    {
        /// <summary>
        /// 获取机器信息
        /// </summary>
        /// <returns></returns>
        public static string GetMachineCode()
        {
            try
            {
                string rs = ToReverse(AESEncryptHelper.Encrypt(GetCpuInfo() + GetMoAddress() + GetHDid()));
                return rs;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 获取CPU序列号
        /// </summary>
        /// <returns>CPU序列号</returns>
        private static string GetCpuInfo()
        {
            string cpuInfo = " ";
            using (ManagementClass cimobject = new ManagementClass("Win32_Processor"))
            {
                ManagementObjectCollection moc = cimobject.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                    mo.Dispose();
                }
            }
            return cpuInfo.ToString();
        }

        /// <summary>
        /// 获取硬盘序列号
        /// </summary>
        /// <returns>硬盘序列号</returns>
        private static string GetHDid()
        {
            string HDid = " ";
            using (ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive"))
            {
                ManagementObjectCollection moc1 = cimobject1.GetInstances();
                foreach (ManagementObject mo in moc1)
                {
                    HDid = (string)mo.Properties["Model"].Value;
                    mo.Dispose();
                }
            }
            return HDid.ToString();
        }

        /// <summary>
        /// 获取MAC地址
        /// </summary>
        /// <returns>MAC地址</returns>
        private static string GetMoAddress()
        {
            string MoAddress = " ";
            using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
            {
                ManagementObjectCollection moc2 = mc.GetInstances();
                foreach (ManagementObject mo in moc2)
                {
                    if ((bool)mo["IPEnabled"] == true)
                        MoAddress = mo["MacAddress"].ToString();
                    mo.Dispose();
                }
            }
            return MoAddress.ToString();
        }




        /// <summary>
        /// 根据字符串生成MD5
        /// </summary>
        /// <param name="str">原字符串</param>
        /// <param name="up">是否大小写</param>
        /// <returns>生成后的MD5</returns>
        public static string To_md5(string str, bool up = false)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string t2 =
            BitConverter.ToString(md5.ComputeHash(
            UTF8Encoding.Default.GetBytes(str)));
            t2 = t2.Replace("-", "");
            if (!up)
                return t2.ToLower();
            else
                return t2.ToUpper();
        }


        public static string ToReverse(string str)
        {
            return new string(str.ToCharArray().Reverse().ToArray());
        }
    }
}
